//
//  UIKit.swift
//  BaseProj
//
//  Created by 陈宇 on 2017/4/26.
//  Copyright © 2017年 科匠武汉. All rights reserved.
//

import UIKit

extension UITableView {
    func hideEmpty() {
        self.tableFooterView = UIView()
    }
}

extension UIButton {
    
    @IBInspectable
    public var backgroundColorForNormal: UIColor {
        get {
            return UIColor.clear
        }
        set {
            setBackgroundImage(UIImage.image(with: newValue, size: CGSize(width: 2, height: 2)), for: .normal)
        }
    }
    
    @IBInspectable
    public var backgroundColorForSelected: UIColor {
        get {
            return UIColor.clear
        }
        set {
            setBackgroundImage(UIImage.image(with: newValue, size: CGSize(width: 2, height: 2)), for: .selected)
        }
    }
    
    @IBInspectable
    public var backgroundColorForHighlighted: UIColor {
        get {
            return UIColor.clear
        }
        set {
            setBackgroundImage(UIImage.image(with: newValue, size: CGSize(width: 2, height: 2)), for: .highlighted)
        }
    }
    
    @IBInspectable
    public var backgroundColorForDisabled: UIColor {
        get {
            return UIColor.clear
        }
        set {
            setBackgroundImage(UIImage.image(with: newValue, size: CGSize(width: 2, height: 2)), for: .disabled)
        }
    }
}

enum Visibility: String {
    case visible
    case invisible
    case gone
}

fileprivate var kUIViewVisibility = "kUIViewVisibility"
fileprivate var kUIViewHeight = "kUIViewHeight"

extension UIView {
    var visibility: Visibility {
        get {
            guard let rawValue = objc_getAssociatedObject(self, &kUIViewVisibility) as? String else {
                return .visible
            }
            return Visibility(rawValue: rawValue)!
        }
        set {
            var oldValue: Visibility = .visible
            if let rawValue = objc_getAssociatedObject(self, &kUIViewVisibility) as? String {
                oldValue = Visibility(rawValue: rawValue)!
            }
            
            if oldValue == newValue {
                return
            }
            
            var heightConstraint: NSLayoutConstraint?
            self.constraints.forEach { (cons) in
                if cons.firstAttribute == .height && cons.secondItem == nil {
                    heightConstraint = cons
                }
            }
            
            objc_setAssociatedObject(self, &kUIViewVisibility, newValue.rawValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
            switch newValue {
            case .visible:
                self.isHidden = false
                if oldValue == .gone {
                    let height = (objc_getAssociatedObject(self, &kUIViewHeight) as? CGFloat) ?? 0
                    if let heightConstraint = heightConstraint {
                        heightConstraint.constant = height
                    } else {
                        var f = self.frame
                        f.size.height = height
                        self.frame = f
                    }
                }
            case .invisible:
                self.isHidden = true
            case .gone:
                self.isHidden = true
                var f = self.frame
                objc_setAssociatedObject(self, &kUIViewHeight, f.height, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                if let heightConstraint = heightConstraint {
                    heightConstraint.constant = 0
                } else {
                    f.size.height = 0
                    self.frame = f
                }
            }
            
        }
    }
}
